﻿using Lan.ServiceCore.Services;
using Lan.ServiceCore.Services.Alarm;
using Lan.ServiceCore.TargetCollection;
using Model;
using RadarToLonLat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lan.ServiceCore.AlarmRecord
{
    public class TrackInfoAdd
    {
        static List<TrackInfoInsert> _trackList = new List<TrackInfoInsert>();
        public static List<TrackInfoInsert> trackList
        {
            get { return _trackList; }
            set { _trackList = value; }
        }
        public static void Init()
        {
            Task.Run(() => { InitSwitchMode(); });

            Task.Run(async () =>
            {
                StartServer();
            });
            Task.Run(async () =>
            {
                Save();
            });
        }
        private static async void InitSwitchMode()
        {
            while (true)
            {
                foreach (WDefenceArea defence in DefenceAreaManager.GetInstance())
                {
                    if (defence == null)
                        continue;
                    defence.CheckPathOvertimeAlarm();
                    defence.CheckPathOvertime();
                }
                await Task.Delay(50);
            }
        }

        private static object _lockDefenceAreas = new object();

        private static void StartServer()
        {
            try
            {
                AlarmService alarmService = new AlarmService();
                while (true)
                {
                    lock (_lockDefenceAreas)
                    {
                        foreach (WDefenceArea defence in DefenceAreaManager.GetInstance())
                        {
                            if (defence.IsAlarmInHand)
                            {

                                List<TargetPath> targetList = new List<TargetPath>();
                                foreach (var item in defence.TargetList)
                                {
                                    targetList.Add(item);
                                }

                                if (targetList.Count == 0)
                                {
                                    continue;
                                }


                                foreach (var item in targetList)
                                {
                                    if (item != null)
                                    {
                                        if (DateTime.Now - item.UpdateTime < TimeSpan.FromSeconds(2))
                                        {
                                            float B_AzimuthAngle = item.NorthDeviationAngle + 90;

                                            Double[] AlarmLonLat = LonLat.GetLonLat(double.Parse(item.Latitude.ToString()), double.Parse(item.Longitude.ToString()), double.Parse(B_AzimuthAngle.ToString()), item.AxesX, item.AxesY, item.AzimuthAngle);
                                            var AlarmID = alarmService.GetInfos(item.DefenceId);
                                            TrackInfoInsert trackInfoModel = new TrackInfoInsert() { AlarmId = AlarmID, UpdateTime = item.UpdateTime, Lat = AlarmLonLat[0], Lng = AlarmLonLat[1], TargetId = item.TargetId, X = item.AxesX, Y = item.AxesY };
                                            _trackList.Add(trackInfoModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Task.Delay(1);
                }
            }
            catch (Exception ex)
            {
            }
        }

        static DateTime _date = DateTime.Now;
        public static async void Save()
        {
            try
            {
                TrackInfoService trackInfoService = new TrackInfoService();

                while (true)
                {
                    DateTime date = DateTime.Now;

                    if ((date - _date).TotalSeconds > 15)
                    {
                        List<TrackInfo> trackListss = new List<TrackInfo>();
                        for (int i = 0; i < trackList.Count; i++)
                        {
                            if (trackList[i] == null)
                            {
                                continue;
                            }
                            TrackInfo trackInfo = new TrackInfo();
                            trackInfo.AlarmId = trackList[i].AlarmId;
                            trackInfo.UpdateTime = trackList[i].UpdateTime;
                            trackInfo.Lat = trackList[i].Lat;
                            trackInfo.Lng = trackList[i].Lng;
                            trackInfo.TargetId = trackList[i].TargetId;
                            trackInfo.X = trackList[i].X;
                            trackInfo.Y = trackList[i].Y;

                            trackListss.Add(trackInfo);
                        }
                        trackList.Clear();
                        if (trackListss.Count > 0)
                        {
                            var list_distinct = trackListss.GroupBy(c => new { c.AlarmId, c.UpdateTime, c.Lat, c.Lng, c.TargetId, c.X, c.Y }).Select(c => c.First()).ToList();
                            int ts = trackInfoService.InsertTrackInfo(list_distinct);

                            _date = DateTime.Now;
                        }

                    }
                    await Task.Delay(1000);
                }
            }
            catch (Exception ex)
            { }
        }
    }
}
